from logrecord.models import LogRecord


class LogManager:

    def add_log(self, request, operation_content, real_user='', remark=''):
        """
            添加用户操作
        """

        method = request.method
        full_path = request.get_full_path()
        username = request.user.username
        if not real_user:
            real_user = username

        try:
            LogRecord.objects.create(
                method=method,
                operation_content=operation_content,
                full_path=full_path,
                message=remark,
                username=username,
                real_user=real_user,
            )
        except Exception as e:
            pass

    def get_log_record(self, kwargs):
        """获取日志"""

        if kwargs:
            return LogRecord.objects.filter(**kwargs).all()

        return LogRecord.objects.all()

    def get_log_records_order_by(self, order_by, kwargs):
        """获取排序后的数据

        :param order_by:排序字段
        """
        return self.get_log_record(kwargs).order_by(order_by)


user_operation_record = LogManager()
